#!/usr/bin/env python
# coding: utf-8
import pyqrcode
import os
import thread
import time
import requests
from traceback import format_exc


def run(web_input, action, bot_list):
    # action ---> http://127.0.0.1/api/*****/  其中×××对应的就是action,通过action字段来实现自定义的操作在下面主程序编写业务逻辑
    # 返回格式如下，code目前为200和500，200为正常，500为异常

    # 查询单个机器人状态
    if action == 'get_wxbot_info':
        try:
            bot_id = web_input['bot_id']
            i = 0
            bot_info_id = None
            for x in bot_list:
                if x.bot_id == bot_id:
                    bot_info_id = i
                    break
                i = i + 1
            if bot_info_id is not None:
                data = {
                    'wx_name': bot_list[bot_info_id].bot.my_account['NickName'],
                    'wx_number': bot_list[bot_info_id].bot.my_account['Uin'],
                    'wx_signature': bot_list[bot_info_id].bot.my_account['Signature'],
                    'wx_info': bot_list[bot_info_id].bot.my_account
                }
                return {'code': 200, 'error_info': '', 'data': data}
            return {'code': 500, 'error_info': 'bot_id not found!', 'data': ''}
        except Exception, e:
            return {'code': 500, 'error_info': str(e), 'data': ''}
